package com.hehui.module.goods.service.impl;

import com.hehui.module.goods.dao.BaseUnitMapper;
import com.hehui.module.goods.dao.RefGoodsUnitMapper;
import com.hehui.module.goods.entity.BaseUnit;
import com.hehui.module.goods.entity.BaseUnitExample;
import com.hehui.module.goods.entity.RefGoodsUnit;
import com.hehui.module.goods.entity.RefGoodsUnitExample;
import com.hehui.module.goods.service.IBaseUnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BaseUnitServiceImpl implements IBaseUnitService {
    @Autowired
    BaseUnitMapper baseUnitMapper; // 单位mapper
    @Autowired
    RefGoodsUnitMapper refGoodsUnitMapper; // 商品中间表mapper

    // 删除商品 逻辑删除
    @Override
    public int delBaseUnit(Long unitId) {
        BaseUnit baseUnit = new BaseUnit();
        // 设置删除 状态为 0
        baseUnit.setIsDel("0");
        baseUnit.setBuUnitid(unitId);
        return baseUnitMapper.updateByPrimaryKeySelective(baseUnit);
    }

    // 根据商品id查询商品单位中间
    @Override
    public List<RefGoodsUnit> findRefGoodsUnitByUnitId(Long unitId) {
        RefGoodsUnitExample example = new RefGoodsUnitExample();
        example.createCriteria().andRefUnitidEqualTo(unitId);
        return refGoodsUnitMapper.selectByExample(example);
    }

    // 根据名字模糊查询
    @Override
    public List<BaseUnit> findBaseUnitByNameLike(String unitName,Long merchantId) {

        return baseUnitMapper.findBaseUnitByNameLike(unitName,merchantId);
    }

    // 添加商品单位
    @Override
    public int addBaseUnit(BaseUnit baseUnit) {
        return baseUnitMapper.insertSelective(baseUnit);
    }

    // 根据商品单位名查询
    @Override
    public List<BaseUnit> findBaseUnitByName(String buName,Long merchantId) {
        BaseUnitExample example = new BaseUnitExample();
        example.createCriteria().andBuNameEqualTo(buName).andBuMerchantidEqualTo(merchantId);
        return baseUnitMapper.selectByExample(example);
    }

    // 查询所有单位 （is_del 字段为1， 0 代表删除）
    // 商户id只查自己的
    @Override
    public List<BaseUnit> findBaseUnit(Long merchantId) {
        BaseUnitExample example = new BaseUnitExample();
        example.createCriteria().andIsDelEqualTo("1").andBuMerchantidEqualTo(merchantId);
        return baseUnitMapper.selectByExample(example);
    }
}
